//
//  CHSortViewController.m
//  CarFile
//
//  Created by 陈浩 on 2017/8/1.
//  Copyright © 2017年 陈浩. All rights reserved.
//

#import "CHSortViewController.h"
#import "CHPickerView.h"
#import "NSDate+Extend.h"
#import "CHLabel.h"
#import "PJ_Model.h"
#import "CHSectionViewCell.h"
#import "CHUploadDataController.h"

@interface CHSortViewController ()<CHPickerViewDelegate,UICollectionViewDelegate,UICollectionViewDataSource,ModuleProtocol,LongriseRouterDelegater>
{
    BOOL isStart;
    NSString *levels;
    NSString *areaid;
    NSString *areacn;
    NSIndexPath *currentIndexpath;
    CGFloat currentHeight;
}
@property (nonatomic ,strong) UILabel *startTimeLabel;


@property (nonatomic ,strong) UICollectionView *collectionView;
@property (nonatomic ,strong) UICollectionView *collectionV;
@property (nonatomic ,copy) LongriseRouterBlock block;
@property (nonatomic ,strong) NSArray *titles;
@end

@implementation CHSortViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"筛选";
        levels = nil;
    self.view.backgroundColor = [UIColor colorWithHexString:@"eeeef0" alpha:1.0f];
    self.titles = @[@"一类",@"二类",@"三类"];
    areaid = (NSString *)[Util getValue:@"areaid"];
    
    UIScrollView *mainScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, UIScreenW, UIScreenH - 40 - 20 - 64)];
    mainScrollView.scrollEnabled = YES;
    [self.view addSubview:mainScrollView];
    
    UILabel *timeTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(22, 0, UIScreenW - 22, 44)];
    timeTitleLabel.text = @"时间筛选";
    timeTitleLabel.font = Font(16);
    timeTitleLabel.backgroundColor = [UIColor clearColor];
    timeTitleLabel.textColor = [UIColor colorWithHexString:@"148ec7" alpha:1.0f];
    [mainScrollView addSubview:timeTitleLabel];
    
    
    
    UIView *headView = [[UIView alloc] initWithFrame:CGRectMake(0, 44, UIScreenW, 60)];
    headView.backgroundColor = [UIColor whiteColor];
    [mainScrollView addSubview:headView];
    
    
    
    UILabel *startTimeLabel = [[UILabel alloc] initWithFrame:CGRectMake(22, 10, 70, 40)];
    startTimeLabel.textColor = [UIColor colorWithHexString:@"333333" alpha:1.0f];
    startTimeLabel.text = @"筛选日期:";
    startTimeLabel.font = Font(14);
    [headView addSubview:startTimeLabel];
    
    CHLabel *startTime = [[CHLabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(startTimeLabel.frame), 15, UIScreenW - CGRectGetMaxX(startTimeLabel.frame) - 10, 30)];
    startTime.userInteractionEnabled = YES;
    startTime.text = @"请选择筛选日期";
    startTime.layer.borderColor = [UIColor lightGrayColor].CGColor;
    startTime.layer.borderWidth = 1;
    startTime.layer.cornerRadius = 3;
    startTime.layer.masksToBounds = YES;
    startTime.textColor = [UIColor colorWithHexString:@"333333" alpha:1.0f];
    startTime.font =Font(14);
    startTime.text = [NSString stringWithFormat:@"%zd-%.2zd",_year,_month];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGesture:)];
    [startTime addGestureRecognizer:tap];
    [headView addSubview:startTime];
    self.startTimeLabel = startTime;
    
    
    UIImageView *startImgView = [[UIImageView alloc] initWithFrame:CGRectMake(startTime.frame.size.width - 20-10, 10, 20, 10)];
    startImgView.image = [UIImage imageNamed:@"down"];
    [startTime addSubview:startImgView];
    
    currentHeight = CGRectGetMaxY(headView.frame);
    
    NSString *usertype = (NSString *)[Util getValue:@"usertype"];
    if (![usertype isEqualToString:@"99"]) {
        UILabel *areaLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, currentHeight, UIScreenW , 40)];
        areaLabel.text = @"      行政区域选择";
        areaLabel.font = Font(14);
        areaLabel.backgroundColor = [UIColor colorWithHexString:@"eeeef0" alpha:1.0f];
        areaLabel.textColor = [UIColor colorWithHexString:@"148ec7" alpha:1.0f];
        [mainScrollView addSubview:areaLabel];
        
        
        NSInteger count = [Globle getInstance].datas.count+1;
        
        NSInteger col = count % 4 == 0 ? count / 4 : count / 4 +1;
        
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
        flowLayout.minimumInteritemSpacing = 1;
        flowLayout.itemSize = CGSizeMake((UIScreenW -10*3 - 40) /4, 30);
        UICollectionView *collectionView =  [[UICollectionView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(areaLabel.frame), UIScreenW,col *50 +20) collectionViewLayout:flowLayout];
        collectionView.delegate = self;
        collectionView.dataSource = self;
        collectionView.scrollEnabled = NO;
        collectionView.pagingEnabled = YES;
        collectionView.backgroundColor = [UIColor whiteColor];
        collectionView.showsHorizontalScrollIndicator = NO;
        [collectionView registerClass:[CHSectionViewCell class] forCellWithReuseIdentifier:@"CHSectionViewCell"];
        [mainScrollView addSubview:collectionView];
        self.collectionView = collectionView;
        currentHeight = CGRectGetMaxY(self.collectionView.frame);
    }
    UILabel *ywLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, currentHeight, UIScreenW , 40)];
    ywLabel.text = @"      业务类别";
    ywLabel.font =Font(14);
    ywLabel.backgroundColor = [UIColor colorWithHexString:@"eeeef0" alpha:1.0f];
    ywLabel.textColor = [UIColor colorWithHexString:@"148ec7" alpha:1.0f];
    [mainScrollView addSubview:ywLabel];
    
    UICollectionViewFlowLayout *Layout = [[UICollectionViewFlowLayout alloc] init];
    Layout.minimumInteritemSpacing = 1;
    Layout.itemSize = CGSizeMake((UIScreenW -10*3 - 40) /4, 30);
    UICollectionView *collectionV=  [[UICollectionView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(ywLabel.frame), UIScreenW,80) collectionViewLayout:Layout];
    collectionV.delegate = self;
    collectionV.dataSource = self;
    collectionV.scrollEnabled = NO;
    collectionV.pagingEnabled = YES;
    collectionV.backgroundColor = [UIColor whiteColor];
    collectionV.showsHorizontalScrollIndicator = NO;
    [collectionV registerClass:[CHSectionViewCell class] forCellWithReuseIdentifier:@"SectionViewCell"];
    [mainScrollView addSubview:collectionV];
    self.collectionV = collectionV;
    
    mainScrollView.contentSize = CGSizeMake(UIScreenW, CGRectGetMaxY(collectionV.frame) +10);
    
    UIButton *sureBtn = [[UIButton alloc] initWithFrame:CGRectMake(22, UIScreenH - 40-10 - 64, UIScreenW - 44, 40)];
    sureBtn.layer.cornerRadius = 5;
    sureBtn.layer.masksToBounds = YES;
    [sureBtn setTitle:@"确定" forState:UIControlStateNormal];
    [sureBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [sureBtn setBackgroundColor:[UIColor colorWithHexString:@"148ec7" alpha:1.0f]];
    [sureBtn addTarget:self action:@selector(sureBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:sureBtn];
    
    
}


-(void)tapGesture:(UITapGestureRecognizer *)tap{
    
    CHPickerView *pickerView = [CHPickerView pickerView];
    pickerView.delegate = self;
    [pickerView show];
    
}


#pragma mark - CHPickerViewDelegate
-(void)pickerViewDidBackToDateStr:(NSString *)dateStr{
    
    self.startTimeLabel.text = dateStr;
    _year = [[dateStr substringToIndex:5] integerValue];
    _month = [[dateStr substringFromIndex:5] integerValue];
    
}





//  确定
-(void)sureBtnClick{
    
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    dict[@"areaid"] = areaid;
    dict[@"levels"] = levels;
    dict[@"year"] =   [NSString stringWithFormat:@"%zd",_year];
    dict[@"month"] =  [NSString stringWithFormat:@"%zd",_month];
    if (self.block) {
        self.block(dict);
    }
    [self.navigationController popViewControllerAnimated:YES];
    
}

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
    
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    if (collectionView == self.collectionView) {
        return  [Globle getInstance].datas.count+1;
    }else{
        return 4;
    }
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    if (collectionView == self.collectionView) {
        static NSString *ID = @"CHSectionViewCell";
        CHSectionViewCell *cell = (CHSectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
        if (indexPath.row == 0) {
            cell.label.text = @"全部";
            cell.backgroundColor = [UIColor colorWithHexString:@"148ec7" alpha:1.0f];
            cell.label.textColor = [UIColor whiteColor];
            [collectionView selectItemAtIndexPath:indexPath animated:NO scrollPosition:UICollectionViewScrollPositionNone];
        }else{
            PJ_Model *model =[Globle getInstance].datas[indexPath.row-1];
            cell.label.text = model.areacn;
        }
        return cell;
    }else{
        
        static NSString *ID = @"SectionViewCell";
        CHSectionViewCell *cell = (CHSectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
        if (indexPath.row == 0) {
            cell.label.text = @"全部";
            cell.backgroundColor = [UIColor colorWithHexString:@"148ec7" alpha:1.0f];
            cell.label.textColor = [UIColor whiteColor];
            [collectionView selectItemAtIndexPath:indexPath animated:NO scrollPosition:UICollectionViewScrollPositionNone];
        }else{
            
            cell.label.text =  self.titles[indexPath.row-1];
        }
        return cell;
    }
    
    
}

-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    
    return UIEdgeInsetsMake(20, 20, 20,20);
}

-(CGFloat )collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section

{
    return 10;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    //    NSLog(@"点击了第%ld组的第%ld个", indexPath.section, indexPath.row);
    
    if (collectionView  == self.collectionView) {
        CHSectionViewCell *cell = (CHSectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
        cell.backgroundColor = [UIColor colorWithHexString:@"148ec7" alpha:1.0f];
        cell.label.textColor = [UIColor whiteColor];
        if (indexPath.row == 0) {
            areaid = (NSString *)[Util getValue:@"areaid"];
        }else{
            PJ_Model *model = [Globle getInstance].datas[indexPath.row-1];
            areaid = model.areaid;
        }
    }else{
        
        CHSectionViewCell *cell = (CHSectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
        cell.backgroundColor = [UIColor colorWithHexString:@"148ec7" alpha:1.0f];
        cell.label.textColor = [UIColor whiteColor];
        if (indexPath.row == 0) {
            levels = @"";
        }else{
            levels = [NSString stringWithFormat:@"%.2zd",indexPath.row];
        }
    }
    
}

- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    CHSectionViewCell *cell =  (CHSectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
    cell.backgroundColor = [UIColor whiteColor];
    cell.label.textColor = [UIColor colorWithHexString:@"148ec7" alpha:1.0f];
    
    
}


#pragma mark LongriseRouterDelegater代理回调方法
-(void)setParams:(id)obj block:(LongriseRouterBlock)block
{
    _block = block;
    if (obj && [obj isKindOfClass:[NSMutableDictionary class]]) {
        
        NSMutableDictionary *dict = obj;
        self.year = [dict[@"year"] integerValue];
        self.month = [dict[@"month"] integerValue];
        
    }
}

#pragma mark ModuleProtocol代理回调方法
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    //处理需要初始化的东西，比如这个是地图模块，这里就实现地图的初始化
    NSLog(@"ModuleViewController2的didFinishLaunchingWithOptions方法被调用了");
    return false;
}

@end
